#ifndef __LATCH_H__
#define __LATCH_H__

#include "mutex.h"
#include "condition.h"
#include "Base/noncopyable.h"

namespace tiny_muduo
{
    class Latch : public NonCopyable
    {
    public:
        explicit Latch(int count) : count_(count), cond_(mutex_) {}
        
        void CountDown()
        {
            MutexLockGuard lock(mutex_);
            --count_;
            if(count_ == 0)
            {
                cond_.BroadCast();
            }
        }

        void Wait()
        {
            MutexLockGuard lock(mutex_);
            while(count_ > 0)
            {
                cond_.Wait();
            }
        }

    private:
        int count_;
        MutexLock mutex_;
        Condition cond_;
    };
}
#endif
